package com.timeDeal.service;



import com.alibaba.druid.util.StringUtils;
import com.timeDeal.bean.PinglunBean;
import com.timeDeal.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 叼爸爸 on 2016/9/6.
 */
public class PinglunServicce {
    public   List<PinglunBean>  getAllPinglunByLeixingId(int id , String leixing , int xianzhi){
        Connection cn = DButil.getConnection();
        int xiaxian = xianzhi*7-7 ;
        int shangxian =  6 ;
        String mysql="select  *   from  pinglun   where   xuqiufabuid=?  limit  "+ xiaxian+",7" ;
        System.out.println(mysql);
        PreparedStatement ps=null;
        System.out.println(id+"sssssssssss");
        ResultSet rs=null;
        List<PinglunBean>  apb = new ArrayList<>() ;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                PinglunBean pb=new PinglunBean();
                pb.setPinglunneirong(rs.getString("pinglunneirong"));
                pb.setPinglunriqi(rs.getDate("pinglunriqi"));
                pb.setPinglunrenid(rs.getInt("pinglunrenid"));
                 apb.add(pb);
            }
            return apb ;
            }
          catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null ;
    }
    public   List<PinglunBean>  getAllPinglunById(int id  , int xianzhi){
        Connection cn = DButil.getConnection();
        int xiaxian = xianzhi*7-7 ;
        String mysql="select  *   from  pinglun   where   shijianchushouid=?  limit  "+ xiaxian+",7" ;
        System.out.println(mysql);
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<PinglunBean>  apb = new ArrayList<>() ;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                PinglunBean pb=new PinglunBean();
                pb.setPinglunneirong(rs.getString("pinglunneirong"));
                pb.setPinglunriqi(rs.getDate("pinglunriqi"));
                pb.setPinglunrenid(rs.getInt("pinglunrenid"));
                apb.add(pb);
            }
            return apb ;
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null ;
    }
    public   String  getCountByLeixingId(String s,int id){
        Connection cn = DButil.getConnection();
        String mysql="select count( *)   from  pinglun   where  xuqiufabuid=?  "   ;
        String mysq2="select count( *)   from  pinglun   where  shijianchushouid=?  "   ;
        System.out.println(mysql);
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            if(StringUtils.equals("xuqiufabu",s)) {
                ps = cn.prepareStatement(mysql);
                ps.setInt(1, id);
                rs = ps.executeQuery();
                while (rs.next()) {
                    return rs.getString(1);
                }
            }else{
                ps = cn.prepareStatement(mysq2);
                ps.setInt(1, id);
                rs = ps.executeQuery();
                while (rs.next()) {
                    return rs.getString(1);
                }
            }
            return  "";
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  "" ;
    }
    public   String  getCountByShijian(int id){
        Connection cn = DButil.getConnection();
        String mysql="select count( *)   from  pinglun   where  shijianchushouid=?  "   ;
        System.out.println(mysql);
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                return   rs.getString(1);
            }
            return  "";
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  "" ;
    }
}
